//
//  UserDao.m
//  LYYGame
//
//  Created by YUJIABO on 17/3/10.
//  Copyright © 2017年 YUJIABO. All rights reserved.
//
#define UserInfo @"UserInfoTable"
#import "UserDao.h"
#import "LYUserModel.h"
#import "DataBaseTool.h"
#import <FMDatabase.h>
#import "SqlStatement+CreateTable.h"
#import "SqlStatement+UserOperation.h"
@implementation UserDao

+(void)createUserInforTable
{
   __block DataBaseTool * tool = [DataBaseTool shareInstance];
    [tool.dbQueue inDatabase:^(FMDatabase *db) {
        
        [db open];
        
        BOOL isExist = [tool checkTableExist:UserInfo withFMData:db];
        
        if (!isExist)
        {
           BOOL result = [db executeUpdate:[SqlStatement createUserInforTable]];
            
            if (result)
            {
                NSLog(@"创建用户信息表成功");
            }
            else
            {
                NSLog(@"创建用户信息表失败");
            }
        }
        
        [db close];
    }];
}

+ (void)addUser:(LYUserModel *)user
{
  __block DataBaseTool * tool = [DataBaseTool shareInstance];
    [tool.dbQueue inTransaction:^(FMDatabase *db, BOOL *rollback) {
        if ([db open])
        {
            NSString *sqlstr = [NSString stringWithFormat:@"select * from %@", UserInfo];
            FMResultSet * Result = [db executeQuery:sqlstr];
            //有数据就跟新 没数据就插入
            if (![Result next])
            {
                [self updateUser:user withDB:db];
            }
            else
            {
                [self insertUser:user withDB:db];
            }
        }
        
        [db close];
    }];
}

+ (void)insertUser:(LYUserModel*)user withDB:(FMDatabase*)db
{

            NSString *sql = [SqlStatement insertUserSql];
            
            [db executeUpdate: sql,user.userId, user.idcarNo, user.password, user.platformId, user.phone, user.qq, user.sex, user.nickname, user.location, user.headIcon,[NSNumber numberWithInt:user.regType],user.forbiddenStartTime,[NSNumber numberWithLongLong:user.lastLoginTime],user.loginAccount,[NSNumber numberWithInt:user.emailStatus],user.headIconSmall,user.address,[NSNumber numberWithInt:user.loginType],user.registerIp,user.lastLoginIp,user.email ,user.channel,user.versionName,user.forbiddenEndTime,[NSNumber numberWithInt:user.osType],user.realName,user.platformName,user.forbiddenRemark,[NSNumber numberWithLongLong:user.createTime],user.age,user.ip,user.versionName,[NSNumber numberWithInt:user.phoneStatus],user.lyb,user.jf,user.lyq];
}

+ (void)updateUser:(LYUserModel*)user withDB:(FMDatabase*)db
{
          NSString *sql = [SqlStatement updateUserSql];
          BOOL success =  [db executeUpdate: sql, user.idcarNo, user.password, user.platformId, user.phone, user.qq, user.sex, user.nickname, user.location, user.headIcon,[NSNumber numberWithInt:user.regType],user.forbiddenStartTime,[NSNumber numberWithLongLong:user.lastLoginTime],user.loginAccount,[NSNumber numberWithInt:user.emailStatus],user.headIconSmall,user.address,[NSNumber numberWithInt:user.loginType],user.registerIp,user.lastLoginIp,user.email ,user.channel,user.versionCode,user.forbiddenEndTime,[NSNumber numberWithInt:user.osType],user.realName,user.platformName,user.forbiddenRemark,[NSNumber numberWithLongLong:user.createTime],user.age,user.ip,user.versionName,[NSNumber numberWithInt:user.phoneStatus],user.lyb,user.jf,user.lyq,user.userId];
         NSLog(@"--------====%d",success);
}

+ (LYUserModel *)getUser;
{
    __block DataBaseTool * tool = [DataBaseTool shareInstance];
    __block LYUserModel * user = nil;
    
    [tool.dbQueue inDatabase:^(FMDatabase *db) {
        
        if ([db open])
        {
            NSString *sql = @"select * from UserInfoTable ";
            FMResultSet *rs = [db executeQuery:sql];
            while ([rs next])
            {
                user = [[LYUserModel alloc]init];
                
                user.userId = [rs stringForColumn:@"userId"];
                user.idcarNo = [rs stringForColumn:@"idcarNo"];
                user.password = [rs stringForColumn:@"password"];
                
                user.platformId =@([[rs stringForColumn:@"platformId"] intValue]);
                user.phone = [rs stringForColumn:@"phone"];
                user.qq = [rs stringForColumn:@"qq"];
                user.sex = [rs stringForColumn:@"sex"];
                user.nickname = [rs stringForColumn:@"nickname"];
                user.location = [rs stringForColumn:@"location"];
                user.headIcon = [rs stringForColumn:@"headIcon"];
                user.regType = [rs intForColumn:@"regType"];
                user.forbiddenStartTime = [rs stringForColumn:@"forbiddenStartTime"];
                user.lastLoginTime = [[rs stringForColumn:@"lastLoginTime"] longLongValue];
                user.loginAccount = [rs stringForColumn:@"loginAccount"];
                user.emailStatus = [rs intForColumn:@"emailStatus"];
                user.headIconSmall = [rs stringForColumn:@"headIconSmall"];
                user.address = [rs stringForColumn:@"address"];
                user.loginType = [rs intForColumn:@"loginType"];
                user.registerIp = [rs stringForColumn:@"registerIp"];
                user.lastLoginIp = [rs stringForColumn:@"lastLoginIp"];
                user.email = [rs stringForColumn:@"email"];
                user.channel = [rs stringForColumn:@"channel"];
                user.versionCode = [rs stringForColumn:@"versionCode"];
                user.forbiddenEndTime = [rs stringForColumn:@"forbiddenEndTime"];
                
                user.osType = [rs intForColumn:@"osType"];
                user.realName = [rs stringForColumn:@"realName"];
                user.platformName = [rs stringForColumn:@"platformName"];
                user.forbiddenRemark = [rs stringForColumn:@"forbiddenRemark"];
                user.createTime = [[rs stringForColumn:@"createTime"] longLongValue];
                user.age = [rs stringForColumn:@"age"];
                user.ip = [rs stringForColumn:@"ip"];
                user.versionName = [rs stringForColumn:@"versionName"];
                user.phoneStatus = [rs intForColumn:@"phoneStatus"];
                
                user.lyb = [rs stringForColumn:@"lyb"];
                user.jf = [rs stringForColumn:@"jf"];
                user.lyq = [rs stringForColumn:@"lyq"];
                
            }
  
        }
        
        [db close];
    }];
    return user;
}
//清空用户表
+ (void)cleanUserInfoTable
{
     __block DataBaseTool * tool = [DataBaseTool shareInstance];
    
    [tool.dbQueue inDatabase:^(FMDatabase *db) {
        
        if ([db open])
        {
            NSString *sqlstr = [NSString stringWithFormat:@"delete from %@", UserInfo];
            BOOL executeResult = [db executeUpdate:sqlstr];
            if (executeResult) {
                NSLog(@"清空用户信息表成功");
            }
        }
        [db close];
    }];
}
@end
